package com.swind.fast.modules.app.halali.partner.service.impl;

import com.swind.fast.common.util.oConvertUtils;

import com.swind.fast.modules.app.halali.partner.entity.RestaurantMealDeal;
// import com.swind.fast.modules.app.halali.partner.entity.RestaurantMealDealBill;
import com.swind.fast.modules.app.halali.partner.mapper.RestaurantMealDealMapper;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantMealDealService;
// import com.swind.fast.modules.app.halali.partner.model.SpaceAnnouncementModel;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;

import javax.annotation.Resource;

import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

import org.springframework.beans.BeanUtils;
import org.apache.shiro.SecurityUtils;
import com.swind.fast.common.system.vo.HalaliLoginUser;
import com.swind.fast.modules.app.halali.partner.vo.request.RankInfoVo;
import com.swind.fast.modules.app.halali.partner.vo.request.DealVo;
import com.swind.fast.modules.app.halali.partner.entity.RestaurantMealDealItem;
import com.swind.fast.modules.app.halali.partner.entity.RestaurantMeal;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantMealService;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantMealDealItemService;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Value;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantDealCategoryService;

/**
 * @Description: 项目
 * @Author: swind
 * @Date:   2021-11-20
 * @Version: V1.0
 */
@Slf4j
@Service
public class RestaurantMealDealServiceImpl extends ServiceImpl<RestaurantMealDealMapper, RestaurantMealDeal> implements IRestaurantMealDealService {

    @Value(value = "${swind.path.preview}")
    private String previewPath;

    @Autowired
    private IRestaurantMealService mealService;

    @Autowired
    private IRestaurantMealDealItemService dealItemService;

    @Autowired
    private IRestaurantDealCategoryService dealCategoryService;

    public RestaurantMealDeal getDetailById(String id) {
        RestaurantMealDeal deal = this.getById(id);
        List<RestaurantMealDealItem> items = this.baseMapper.getDealItems(id);
        List<String> tids = items.stream().map(RestaurantMealDealItem::getMealId).collect(Collectors.toList());
        if (oConvertUtils.listIsNotEmpty(tids)) {
            List<RestaurantMeal> meals = this.mealService.listByIds(tids);
            for (RestaurantMeal meal : meals) {
                Optional<RestaurantMealDealItem> itemOpt = items.stream().filter(p -> p.getMealId().equals(meal.getId())).findFirst();
                if (itemOpt.isPresent()) {
                    RestaurantMealDealItem dealItem = itemOpt.get();
                    if (oConvertUtils.isNotEmpty(dealItem.getQuantity())) {
                        meal.setQuantity(dealItem.getQuantity());
                    }
                }
            }
            deal.setItems(meals);
        }

        this.renderPic(deal);
        return deal;
    }

    public List<RestaurantMealDeal> getDealList() {
        HalaliLoginUser sysUser = (HalaliLoginUser) SecurityUtils.getSubject().getPrincipal();
        String id = sysUser.getRestaurantId();
        List<RestaurantMealDeal> list = this.baseMapper.getDealList(id);
        this.renderPicList(list);
        return list;
    }

    public DealVo saveMain(DealVo dealVo) {
        RestaurantMealDeal deal = new RestaurantMealDeal();
        String[] ingoreProperties = {"id"};
        oConvertUtils.copyPropertiesIgnoreNull(dealVo, deal, ingoreProperties);
        HalaliLoginUser sysUser = (HalaliLoginUser) SecurityUtils.getSubject().getPrincipal();
        String restId = sysUser.getRestaurantId();
        deal.setRestaurantId(restId);
        this.save(deal);
        dealVo.setId(deal.getId());
        this.dealCategoryService.saveCategories(deal.getId(), dealVo);
        this.dealItemService.saveOrUpdateDealItems(restId, dealVo);
        return dealVo;
    }

    public DealVo updateInfo(DealVo dealVo) {
        RestaurantMealDeal deal = this.getById(dealVo.getId());
        oConvertUtils.copyPropertiesIgnoreNull(dealVo, deal);
        this.updateById(deal);
        this.dealCategoryService.saveCategories(deal.getId(), dealVo);
        this.dealItemService.saveOrUpdateDealItems(deal.getRestaurantId(), dealVo);
        oConvertUtils.copyPropertiesIgnoreNull(deal, dealVo);
        return dealVo;
    }

    public void updateRank(List<RankInfoVo> items) {
        List<RestaurantMealDeal> list = new ArrayList<RestaurantMealDeal>();
        for (RankInfoVo item : items) {
            list.add(oConvertUtils.entityToModel(item, RestaurantMealDeal.class));
        }
        this.updateBatchById(list);
    }

    public void renderPic(RestaurantMealDeal deal) {
        if (oConvertUtils.isEmpty(deal.getPic()) || deal.getPic().startsWith("http")) {
            return;
        }
        String picPath = this.previewPath + "/" + deal.getPic();
        deal.setPic(picPath);
    }

    private void renderPicList(List<RestaurantMealDeal> deals) {
        for (RestaurantMealDeal deal : deals) {
            this.renderPic(deal);
        }
    }

    public void removeDealById(String id) {
        this.removeById(id);
        HalaliLoginUser sysUser = (HalaliLoginUser) SecurityUtils.getSubject().getPrincipal();
        String restId = sysUser.getRestaurantId();
        this.baseMapper.removeDealItems(restId, id);
    }

}
